package com.sunshine.rest.controller;

import com.sunshine.rest.bean.Person;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * FirstController class
 *
 * @author WeiHong
 * @date 2018/11/9
 */
@RestController
public class FirstController {

    @RequestMapping(value = "/findPerson/{personId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Person findPerson(@PathVariable("personId") Integer personId, HttpServletRequest request) {
        Person person = new Person(personId, "Liming", 22);
        person.setMessage(request.getRequestURL().toString());
        return person;
    }

    @GetMapping("hello")
    public String hello() {
        return "hello world";
    }


    @PostMapping(value = "/person/create", consumes = MediaType.APPLICATION_JSON_VALUE)
    public String createPerson(@RequestBody Person person) {
        System.out.println("person.toString() = " + person.toString());
        return "Success ! person.id = " + person.getId();
    }


    @PostMapping(value = "/person/createXML",
            consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
    public String createXmlPerson(@RequestBody Person person) {
        System.out.println("person.toString() = " + person.toString());
        return "<result><message>success</message></result>";
    }
}
